package com.fhz.kotlintest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.core.view.GravityCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.fhz.kotlintest.databinding.ActivityKotlinBinding
import com.google.android.material.snackbar.Snackbar

class KotlinActivity : AppCompatActivity() {
    private lateinit var binding:ActivityKotlinBinding
    private val TAG = "KotlinActivity"
    private val viewModel by lazy {
        ViewModelProvider(this).get(FoodViewModel::class.java)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityKotlinBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)
        viewModel.getFood()

        //适配器
        val layoutManager = GridLayoutManager(this,2)
        binding.recycleView.layoutManager = layoutManager

        val list = ArrayList<Data>()
        viewModel.food.observe(this,{
            Log.i(TAG, "onCreate: "+it.data.size)
            val adapter = RVAdapter(R.layout.rv_layout,it.data)
            binding.recycleView.adapter = adapter
        })


        // bar
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.baseline_menu_black_24dp)
        }

        binding.navView.setCheckedItem(R.id.navCall)
        binding.navView.setNavigationItemSelectedListener {
            binding.drawerLayout.closeDrawers()
            when(it.itemId){
                R.id.navCall -> {
                    Toast.makeText(this,"打电话",Toast.LENGTH_SHORT).show()
                }
                R.id.navEmail -> {
                    Toast.makeText(this,"发邮件",Toast.LENGTH_SHORT).show()
                }
            }
            true
        }

        binding.fab.setOnClickListener {
//            Toast.makeText(this,"fab",Toast.LENGTH_SHORT).show()
            Snackbar.make(it,"data",Snackbar.LENGTH_SHORT)
                    .setAction("Undo"){
                        Toast.makeText(this,"fab",Toast.LENGTH_SHORT).show()
            }.show()
        }
    }



    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.backup -> Toast.makeText(this,"backup",Toast.LENGTH_SHORT).show()
            R.id.delete -> Toast.makeText(this,"delete",Toast.LENGTH_SHORT).show()
            android.R.id.home -> {
                Toast.makeText(this,"home",Toast.LENGTH_SHORT).show()
                binding.drawerLayout.openDrawer(GravityCompat.START)
            }
        }
        return true
    }
}